home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / Polygon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  8.0 KB  |  242 lines

  1. /*
  2. //   (C) COPYRIGHT International Business Machines Corp. 1993
  3. //   All Rights Reserved
  4. //   Licensed Materials - Property of IBM
  5. //   US Government Users Restricted Rights - Use, duplication or
  6. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. //
  8.  
  9. //
  10. // Permission to use, copy, modify, and distribute this software and its
  11. // documentation for any purpose and without fee is hereby granted, provided
  12. // that the above copyright notice appear in all copies and that both that
  13. // copyright notice and this permission notice appear in supporting
  14. // documentation, and that the name of I.B.M. not be used in advertising
  15. // or publicity pertaining to distribution of the software without specific,
  16. // written prior permission. I.B.M. makes no representations about the
  17. // suitability of this software for any purpose.  It is provided "as is"
  18. // without express or implied warranty.
  19. //
  20. // I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
  22. // BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  24. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  25. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. // Author:  John Spitzer, IBM AWS Graphics Systems (Austin)
  28. //
  29. */
  30.  
  31. #include <math.h>
  32. #include "Polygon.h"
  33. #define sq(val)    ((val)*(val))
  34.  
  35. #undef offset
  36. #ifdef WIN32
  37. #define offset(v) offsetof(PolygonStr,v)
  38. #else
  39. #define offset(v) offsetof(Polygon,v)
  40. #endif
  41.  
  42. static InfoItem PolygonInfo[] = {
  43. #define INC_REASON INFO_ITEM_ARRAY
  44. #include "Polygon.h"
  45. #undef INC_REASON
  46. };
  47. #include <malloc.h>
  48.  
  49. PolygonPtr new_Polygon()
  50. {
  51. #ifdef WIN32
  52.     PolygonPtr this = (PolygonPtr)malloc(sizeof(PolygonStr)); /* Name conflict */
  53. #else
  54.     PolygonPtr this = (PolygonPtr)malloc(sizeof(Polygon));
  55. #endif
  56.     CheckMalloc(this);
  57.     new_Polygonal((PolygonalPtr)this);
  58.     SetDefaults((TestPtr)this, PolygonInfo);
  59.     this->testType = PolygonTest;
  60.     this->primitiveType = GL_POLYGON;
  61.     this->vertsPerFacet = this->numSides;
  62.     this->objsPerBgnEnd = 1;
  63.     this->usecPixelPrint = " microseconds per pixel with Polygon";
  64.     this->ratePixelPrint = " pixels per second with Polygon";
  65.     this->usecPrint = " microseconds per Polygon";
  66.     this->ratePrint = " Polygons per second";
  67.     /* Set virtual functions */
  68.     this->SetState = Polygon__SetState;
  69.     this->delete = delete_Polygon;
  70.     this->Layout = Polygon__Layout;
  71.     this->Copy = Polygon__Copy;
  72.     return this;
  73. }
  74.  
  75. void delete_Polygon(TestPtr thisTest)
  76. {
  77.     PolygonPtr this = (PolygonPtr)thisTest;
  78.     delete_Polygonal(thisTest);
  79. }
  80.  
  81. int Polygon__SetState(TestPtr thisTest)
  82. {
  83.     PolygonPtr this = (PolygonPtr)thisTest;
  84.  
  85.     /* set parent state */
  86.     if (Polygonal__SetState(thisTest) == -1) return -1;
  87.  
  88.     /* set own state */
  89.     /* Nothing needs to be set, but we need to see if the number
  90.        of sides of the polygon can be drawn with whatever amount
  91.        of loop unrolling is set up in the Vertex */
  92.     
  93.     if (this->numSides > 8 && this->loopUnroll > 1 ||
  94.         this->loopUnroll % this->numSides != 0 &&
  95.         this->numSides % this->loopUnroll != 0)
  96.         return -1;
  97.  
  98.     /* This is a bit of a hack.  When ObjsPerBeginEnd is applied to
  99.      * a PolygonTest, it overwrites the setting in the creator above
  100.      * which correctly sets this->objsPerBgnEnd to 1.  We must make
  101.      * sure that it is set to 1, or the traveral routine gets confused
  102.      * and will not draw all the polygons.  Therefore, we'll set
  103.      * objsPerBgnEnd again here. 
  104.      */
  105.     this->objsPerBgnEnd = 1;
  106.  
  107.     return 0;
  108. }
  109.  
  110. TestPtr Polygon__Copy(TestPtr thisTest)
  111. {
  112.     PolygonPtr this = (PolygonPtr)thisTest;
  113.     PolygonPtr newPolygon = new_Polygon();
  114.     FreeStrings((TestPtr)newPolygon);
  115.     *newPolygon = *this;
  116.     CopyStrings((TestPtr)newPolygon, (TestPtr)this);
  117.     return (TestPtr)newPolygon;
  118. }
  119.  
  120. void Polygon__Layout(VertexPtr thisVertex)
  121. {
  122.     PolygonPtr this = (PolygonPtr)thisVertex;
  123.     const double pi=3.141592654;
  124.     double ndcSize, lengthSide, halfWidth, radius;
  125.     double theta, theta0, deltaTheta;
  126.     GLfloat x, y;
  127.     GLfloat *src, *dst;
  128.     GLfloat *newTraversalData;
  129.     int numFrontIn, numFrontOut, numFrontClip;
  130.     int numBackIn, numBackOut, numBackClip;
  131.     int frontIn, frontOut, frontClip, backIn, backOut, backClip;
  132.     GLfloat face=1.0;
  133.     int i, j;
  134.     GLfloat facingFront = this->facingFront;
  135.     GLfloat facingBack  = this->facingBack;
  136.     int   numObjects  = this->numObjects;
  137.     GLfloat acceptObjs  = this->acceptObjs;
  138.     GLfloat rejectObjs  = this->rejectObjs;
  139.     GLfloat clipObjs     = this->clipObjs;
  140.     int   windowDim   = min(this->environ.windowWidth, this->environ.windowHeight);
  141.     int   orientation = this->orientation;
  142.     GLfloat size        = this->size;
  143.     int   objsPerBgnEnd  = this->objsPerBgnEnd;
  144.  
  145.     this->vertsPerFacet = this->numSides;
  146.  
  147.     /* These will/could be used in traversal function */
  148.     this->vertsPerBgnEnd = this->vertsPerFacet;
  149.     this->facetsPerBgnEnd = 1;
  150.  
  151.     /* Figure front facing/back facing stuff */
  152.     if (fabs(1.0 - facingFront - facingBack) > .01) {
  153.     printf("FrontFacing + BackFacing not equal to 1\n");
  154.     exit(1);
  155.     }
  156.     numFrontIn = floor((GLfloat)numObjects * acceptObjs * facingFront + 0.5);
  157.     numFrontOut = floor((GLfloat)numObjects * rejectObjs * facingFront + 0.5);
  158.     numFrontClip = floor((GLfloat)numObjects * clipObjs * facingFront + 0.5);
  159.     numBackIn = floor((GLfloat)numObjects * acceptObjs * facingBack + 0.5);
  160.     numBackOut = floor((GLfloat)numObjects * rejectObjs * facingBack + 0.5);
  161.     numBackClip = floor((GLfloat)numObjects * clipObjs * facingBack + 0.5);
  162.     numFrontIn += numObjects - numFrontIn - numFrontOut - numFrontClip
  163.                              - numBackIn - numBackOut - numBackClip;
  164.     frontIn = frontOut = frontClip = backIn = backOut = backClip = 0;
  165.  
  166.     /* Figure polygon dimensions given polygon size in pixels and number of sides */
  167.     ndcSize = size/(double)windowDim/(double)windowDim*4.0;
  168.     halfWidth = sqrt(ndcSize/(double)this->numSides/tan(pi/(double)this->numSides));
  169.     lengthSide = 2.0*ndcSize/halfWidth/(double)this->numSides;
  170.     radius = sqrt(sq(lengthSide/2.0) + sq(halfWidth));
  171.     deltaTheta = 2.0*pi/(double)this->numSides;
  172.  
  173.     /* Use Vertex__Layout() to get polygon centers */
  174.     this->layoutPoints = numObjects;
  175.     this->layoutPadding = radius;
  176.     this->layoutPadding += 1./(float)windowDim;
  177.     Vertex__Layout(thisVertex);
  178.  
  179.     /* Allocate necessary data */
  180.     src = this->traversalData;
  181.     newTraversalData = (GLfloat*)malloc(numObjects * this->numSides * 2 * sizeof(GLfloat));
  182.     CheckMalloc(newTraversalData);
  183.     dst = newTraversalData;
  184.  
  185.     /* Figure initial theta position for orientation */
  186.     switch (orientation) {
  187.     case Horizontal:
  188.         theta0 = 3.0*pi/2.0 - deltaTheta/2.0;
  189.         break;
  190.     case Vertical:
  191.         theta0 = pi - deltaTheta/2.0;
  192.         break;
  193.     case Random:
  194.             mysrand(15000);
  195.         break;
  196.     }
  197.  
  198.     /* Generate those vertices around the point centers */
  199.     for (i=0; i<numObjects; i++) {
  200.     x = *src++;
  201.     y = *src++;
  202.     if (orientation == Random)
  203.             theta0 = 2.0*pi*(double)myrand()/(double)MY_RAND_MAX;
  204.     if ((x==-1 || x==1) && (-1<=y && y<=1) || (y==-1 || y==1) && (-1<=x && x<=1)) {
  205.         /* The vertex is clipped (on the viewport boundary) */
  206.         if (frontClip < numFrontClip) {
  207.         frontClip++;
  208.         face = 1.0;
  209.         } else {
  210.         backClip++;
  211.         face = -1.0;
  212.         }
  213.     } else if (-1<=x && x<=1 && -1<=y && y<=1) {
  214.         /* The vertex is in the viewport */
  215.         if (frontIn < numFrontIn) {
  216.         frontIn++;
  217.         face = 1.0;
  218.         } else {
  219.         backIn++;
  220.         face = -1.0;
  221.         }
  222.     } else {
  223.         /* The vertex is outside the viewport */
  224.         if (frontOut < numFrontOut) {
  225.         frontOut++;
  226.         face = 1.0;
  227.         } else {
  228.         backOut++;
  229.         face = -1.0;
  230.         }
  231.     }
  232.     theta = theta0;
  233.     for (j=0; j<this->numSides; j++) {
  234.         *dst++ = radius * cos(theta) + x;
  235.         *dst++ = radius * sin(theta) + y;
  236.         theta += face*deltaTheta;
  237.     }
  238.     }
  239.     free(this->traversalData);
  240.     this->traversalData = newTraversalData;
  241. }
  242.